package site.beanyon.now.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import site.beanyon.now.model.po.UserPO;

import java.util.List;

/**
 * 定时任务服务实现类
 */
@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private TimeCardService timeCardService;

    @Autowired
    private TimePlanService timePlanService;

    @Autowired
    private UserService userService;

    /**
     * 为所有用户创建其时间计划中的时间卡片，每日0点0分0秒执行一次
     */
    @Scheduled(cron = "0 0 0 * * *")
    @Override
    public void createDefaultTimePlanEveryDay() {
        List<UserPO> userList = this.userService.getAllUsers();

        // 遍历用户，根据每个用户的时间计划，为用户创建当日的时间卡片
        for (UserPO userPO : userList) {
            Integer userId = userPO.getId();
            // 获取每个用户的时间计划Activity列表
            List<String> activityList = this.timePlanService.listTimePlanActivity(userId);
            // 遍历用户的Activity列表，创建时间卡片
            for (String activity : activityList) {
                // 创建时间卡片
                this.timeCardService.createTimeCardWithActivity(userId, activity);
            }
        }
    }
}
